﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.Infrastructure.Authentication;
using XSTDZ.ERP.WebApp.Models;
using XSTDZ.ERP.Core.Extentions;
using XSTDZ.ERP.WebApp.ViewModel;
namespace XSTDZ.ERP.WebApp.Controllers
{
    //字码表控制器
    public class RefeController : Controller
    {
        private readonly IRefeService refeServiceImpl = ServiceLocator.Instance.GetService<IRefeService>();	
        //
        // GET: /Refe/

        public ActionResult Index()
        {
            return View();
        }
        #region 添加字码表选项
        [NoAuthorizated("AddRefe")]
        public ActionResult AddRefe(Guid refeTypeId)
        {
            ViewBag.RefeTypeId = refeTypeId;
            return View();
        }
        [HttpPost]
        [NoAuthorizated("AddRefe")]
        public ActionResult AddRefe(RefeDto refeDto)
        {
            Guid refeTypeId;
            Guid.TryParse(Request.Form["RefeTypeId"].ToString(),out refeTypeId);
            if (ModelState.IsValid)
            {
                refeServiceImpl.AddRefe(refeTypeId, refeDto);
                ViewBag.PostState = 9;
            }
            return View();
        }
        #endregion
        #region 更新字码表状态（不能更新信息）
        [HttpGet]
        [NoAuthorizated("EditRefe")]
        public ActionResult EditRefe(Guid refeId)
        {
             refeServiceImpl.UpdateRefeState(refeId);
            return Content("ok");
        }
        #endregion
        #region 辅助显示

        [NoAuthorizated("GetAllRefeTypes")]
        public ActionResult GetAllRefeTypes()
        {
            var refeTypes = refeServiceImpl.GetAllRefeTypeDtos().Where(t=>t.IsEnableEdit);
            if (refeTypes != null && refeTypes.Count() != 0)
            {
                //var jsonResult = refeTypes.Select(u => new
                //{
                //    Key = u.Id,
                //    Name = u.TypeName,
                //    ModuleName = u.ModuleName,
                //    ChildCounts = u.RefeDtos == null ? 0 : u.RefeDtos.Where(r => r.Deleted != 1).Count()
                //});
                var jsonResult = new List<RefeViewModel>();
                refeTypes.Each(r => r.RefeDtos.Each(t => { if (t.IsEnableEdit) { jsonResult.Add(new RefeViewModel { Id = t.Id, RefeName = t.RefeName, RefeCode = t.RefeCode, TypeName = r.TypeName, TypeModuleName = r.ModuleName, TypeId = r.Id, State = t.Deleted == 0 ? "正常" : "删除" }); } }));
                //foreach (var item in refeTypes)
                //{ 
                //    item.RefeDtos.Each(
                //}
                //var jsonResult = new RefeModel().ConvertToUiData(refeTypes);

                var target = Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult);
                target = "{\"Rows\":" + target + ",\"Total\":\"" + refeTypes.Count() + "\"}";
                return Content(target);
            }
            return Content("");
        }
        #endregion
    }
}
